﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.User.Contact
{
    using SharingCenter.Web.Classes;

    public partial class AddContact : PageBase
    {
        protected Guid targetUserId;
        protected Sc_Profile targetProfile;
        protected bool proceed = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null)
            {
                RedirectToDefaultPage();
            }

            GetMemUser(targetUserId);
            if (proceed)
            {
                RenderPage();
            }
            else
            {
                Panel1.Visible = false;
                pnFail.Visible = true;
                lnkReturn.NavigateUrl = Request.UrlReferrer != null ? Request.UrlReferrer.PathAndQuery :
                    "javascript:history.go(-1)";
            }
        }

        void RenderPage()
        {
            ltrTitle.Text = string.Format("添加好友: {0}", targetProfile.NickName);
            msgAdding.Text = string.Format("你将要添加<strong>{0}</strong>为你的好友。", targetProfile.NickName);
            aiTarget.UserId = targetUserId;
            lblTargetNickName.Text = targetProfile.NickName;
        }

        void GetMemUser(Guid userId)
        {
            try
            {
                targetUserId = new Guid(Request.QueryString["id"]);
                using (scDataContext cxt = new scDataContext())
                {
                    targetProfile = cxt.Sc_Profiles.Single(d => d.UserId == targetUserId);
                }
                
                if (targetUserId == Profile.UserId)
                {
                    msgAdding.MessageType = MessageBoxTypes.Warning;
                    msgAdding.Text = "不能添加自己为好友";
                    ltrTitle.Text = "不能添加自己为好友。";
                    proceed = false;
                }
                else
                {

                }
            }
            catch
            {
                msgAdding.MessageType = MessageBoxTypes.Error;
                msgAdding.Text = "错误的用户号。";
                ltrTitle.Text = "错误的用户号。";
                proceed = false;
            }

            
        }
    }
}
